package parser;

import java.io.*;
import java.util.*;
import java.util.zip.GZIPInputStream;

import data.CityArea;
import data.MatrixTime;


public class ErlangParser {
	
	public static MatrixTime parse(String in_file, CityArea area) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(in_file))));
		
		/*
		Read Header:
		HOUR 0
		MINUTES 0
		YEAR 2010
		MONTH 7
		DAY 31
		CITY Piemonte;AreaMetroTO
		NROWS 172
		NCOLS 193
		ULXMAP 7.3992778712
		ULYMAP 45.2506248811
		XDIM 0.002777778
		YDIM 0.0020833335000000003
		CELLE_TRUE 2889
		CELLE_FALSE 0
		*/
		
		int hour = Integer.parseInt(br.readLine().trim().substring("HOUR".length()+1).trim());
		int minutes = Integer.parseInt(br.readLine().trim().substring("MINUTES".length()+1).trim());
		int year = Integer.parseInt(br.readLine().trim().substring("YEAR".length()+1).trim());
		int month = Integer.parseInt(br.readLine().trim().substring("MONTH".length()+1).trim()) - 1; // JANUARY = 0 in java;
		int day = Integer.parseInt(br.readLine().trim().substring("DAY".length()+1).trim());
		
		
		String city = br.readLine().trim();
		int nrows = Integer.parseInt(br.readLine().trim().substring("NROWS".length()+1).trim());
		int ncols = Integer.parseInt(br.readLine().trim().substring("NCOLS".length()+1).trim());
		double ulxmap = Double.parseDouble(br.readLine().trim().substring("ULXMAP".length()+1).trim());
		double ulymap = Double.parseDouble(br.readLine().trim().substring("ULYMAP".length()+1).trim());
		double xdim = Double.parseDouble(br.readLine().trim().substring("XDIM".length()+1).trim());
		double ydim = Double.parseDouble(br.readLine().trim().substring("YDIM".length()+1).trim());
		int celle_true = Integer.parseInt(br.readLine().trim().substring("CELLE_TRUE".length()+1).trim());
		int celle_false = Integer.parseInt(br.readLine().trim().substring("CELLE_FALSE".length()+1).trim());
		
		//System.out.println((ulymap-ydim*nrows)+","+ulxmap);
		//System.out.println(ulymap+","+(ulxmap+xdim*ncols));
		
		
		Calendar start = new GregorianCalendar(year,month,day,hour,minutes);
		Calendar end = new GregorianCalendar(year,month,day,hour,minutes+15);
		
		if(hour == 0 && minutes == 0){
			start.set(Calendar.DAY_OF_MONTH, day-1);
			end.set(Calendar.DAY_OF_MONTH, day-1);
			
		}
		
		double[][] mat = new double[area.getNrows()][area.getNcols()];
		
		// dump not relevant rows
		for(int i=0; i<area.getMini();i++)
			br.readLine();
		
		for(int i=area.getMini(); i<area.getMaxi(); i++) {
			String[] row = br.readLine().trim().split("\t");
			for(int j=area.getMinj(); j<area.getMaxj(); j++) {
				 double x = Double.parseDouble(row[j]);
				 mat[i-area.getMini()][j-area.getMinj()] = x;
			}
			
		}
		
		br.close();
		return new MatrixTime(mat,start,end,area);
	}
	
	

	
	// 200912010000
	public static Calendar parseDateFileName(String date) {
		Integer year = Integer.parseInt(date.substring(0,4));
		Integer month = Integer.parseInt(date.substring(4,6)) - 1; // JANUARY = 0 in java
		Integer day = Integer.parseInt(date.substring(6,8));
		Integer hour = Integer.parseInt(date.substring(8,10));
		Integer minutes = Integer.parseInt(date.substring(10,12));
		return new GregorianCalendar(year,month,day,hour,minutes);	
	}
	
	
}
